% 原始数据
x = [1, 2, 3, 4, 5, 6, 7, 8];
f = [3, 4, 7, 4, 3, 5, 6, 12];

% 插值点
xi = 1:0.1:8;  % 从1到8以0.1为步长进行插值

% 线性插值
yi = interp1(x, f, xi, 'linear');

% 绘制原始数据和插值结果
figure;
plot(x, f, 'o', xi, yi, '-')
legend('data point', 'linear interpolation')
xlabel('x')
ylabel('y')

ylim([0, 13])

x = -3:0.01:3;  % 定义 x 的取值范围

% 计算函数值
g = zeros(size(x));
idx1 = abs(x) <= 1;
idx2 = abs(x) > 1 & abs(x) <= 2;
g(idx1) = cos(pi/2 * x(idx1));
g(idx2) = -pi/2 * (abs(x(idx2)).^3 - 5 * abs(x(idx2)).^2 + 8 * abs(x(idx2)) - 4);

% 绘制图像
figure;
plot(x, g)
xlabel('x')
ylabel('g(x)')
title('Plot of g(x)')
grid on

f = [3 4 7 4 3 5 6 12];
x = -10:0.01:10;  % 定义 x 的取值范围
% 定义函数 g(x)
g = @(x) cos(pi/2*x).*(abs(x)<=1) - (pi/2)*(abs(x).^3 - 5*abs(x).^2 + 8*abs(x) - 4).*(abs(x)>1 & abs(x)<=2);
% 计算 F_g(x)
F_g = zeros(size(x));
for i = 1:length(f)
    F_g = F_g + g(x-i) .* f(i);
end

% 绘制图像
plot(x, F_g)
xlabel('x')
ylabel('F_g(x)')
title('Plot of F_g(x)')
grid on

dx = x(2) - x(1);  % Compute the step size
dF_g = diff(F_g) / dx;  % Estimate the derivative using finite differences

% Plot the derivative
figure;
plot(x(1:end-1), dF_g)
xlabel('x')
ylabel('dF_g(x)/dx')
title('Plot of the derivative of F_g(x)')
grid on